package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.po.Farmer;
import com.example.domain.po.LycheeVariety;
import com.example.domain.po.ProductionArea;
import com.example.domain.vo.CityOutput;
import com.example.domain.vo.RegionInformation;
import com.example.service.FarmerService;
import com.example.service.LycheeVarietyService;
import com.example.service.ProductionAreaService;
import com.example.mapper.ProductionAreaMapper;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author adim
* @description 针对表【production_area(荔枝产区信息表，存储地理坐标及主栽品种)】的数据库操作Service实现
* @createDate 2025-06-10 17:20:32
*/
@Service
@RequiredArgsConstructor
public class ProductionAreaServiceImpl extends ServiceImpl<ProductionAreaMapper, ProductionArea>
    implements ProductionAreaService{

    private final ProductionAreaMapper productionAreaMapper;

    private final LycheeVarietyService lycheeVarietyService;

    private final FarmerService farmerService;
    /**
     * 获取城市生产荔枝总量
     * @return
     */
    @Override
    public List<CityOutput> getTopCity() {

        return productionAreaMapper.getTopCity();
    }

    /**
     * 获取指定城市所有厂区信息
     * @param city
     * @return
     */
    @Override
    public List<RegionInformation> list(String city) {

        List<ProductionArea> productionAreaList = this.lambdaQuery()
                .eq(city != null,ProductionArea::getCity, city)
                .list();

        List<RegionInformation> regionInformationList = new ArrayList<>();
        productionAreaList.stream().map(productionArea -> {
            RegionInformation regionInformation = new RegionInformation();
            Farmer farmer = farmerService.getByRegionId(productionArea.getId());
            BeanUtil.copyProperties(productionArea,regionInformation);
            LycheeVariety lycheeVariety =lycheeVarietyService.getLycheeById(productionArea.getMainVarietyId());
            regionInformation.setVarietyName(lycheeVariety.getVarietyName());
            regionInformation.setImageUrl(lycheeVariety.getImageUrl());
            BeanUtil.copyProperties(farmer,regionInformation);
            regionInformationList.add(regionInformation);
            return regionInformation;
        }).collect(Collectors.toList());

        return regionInformationList;
    }
}




